﻿using Gecko.Entities;
using Gecko.Models;
using NHibernate;
using NHibernate.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gecko.Respositories
{
    public class WaterLeakRepository : RepositoryBase<WaterLeak>
    {
        private NHibernate.ISession _session;

        public WaterLeakRepository(ISession _session)
            : base(_session)
        {
        }

        public WaterLeak GetByLatLong(double lat, double lon)
        {
            return Session.Query<WaterLeak>().Where(p=>p.LatPoint == lat && p.LongPoint == lon).FirstOrDefault();
        }

        public IList<ShortWaterLeak>GetShortedWaterleak(double lat, double lon)
        {
            var result = Session.CreateSQLQuery("exec findShortedWaterLeak :lat, :long")
                    .AddEntity(typeof(ShortWaterLeak))
                    .SetParameter("lat", lat)
                    .SetParameter("long", lon)
                    .List<ShortWaterLeak>();
            return result;
        } 


        public IList<WaterLeakWithFirstResourceUrl> GetAllWithResource()
        {
            var getAllResult = this.GetAll();
            var result = getAllResult.Select(p => new WaterLeakWithFirstResourceUrl
            {
                Detail = p.Detail,
                Id = p.Id,
                LatPoint = p.LatPoint,
                LongPoint = p.LongPoint,
                Status = p.Status,
                State = (p.Histories != null && p.Histories.Count > 0) ? p.Histories.Last().State : "",
                Topic = p.Topic,
                Date = (p.Histories!=null&& p.Histories.Count>0)?p.Histories.Last().CreateDate.ToString("dd/MM/yyyy hh:mm tt"): "",
                UserId = (p.Histories!=null&& p.Histories.Count>0)?p.Histories.Last().UserInfo.Username : String.Empty,
                UserName = (p.Histories != null && p.Histories.Count > 0) ? p.Histories.Last().UserInfo.Name : String.Empty,
                Url = (p.Resources.FirstOrDefault() != null) ? p.Resources.First().Id.ToString() : ""
            }).ToList();

            return result;
        }

        public WaterLeakWithFirstResourceUrlFull GetByIdWithResource(int id)
        {
            WaterLeakWithFirstResourceUrlFull result = null;
            WaterLeak waterLeak = this.Get(id);
            if (waterLeak != null)
            {
                var resource1 = "";
                var resource2 = "";
                var resource3 = "";
                if (waterLeak.Resources != null)
                {
                    resource1 = (waterLeak.Resources[0] != null) ? waterLeak.Resources[0].Id.ToString() : "";
                    resource2 = (waterLeak.Resources.Count > 2 && waterLeak.Resources[1] != null) ? waterLeak.Resources[1].Id.ToString() : "";
                    resource3 = (waterLeak.Resources.Count > 3 && waterLeak.Resources[2] != null) ? waterLeak.Resources[2].Id.ToString() : "";
                }

                result = new WaterLeakWithFirstResourceUrlFull
                {
                    Detail = waterLeak.Detail,
                    Id = waterLeak.Id,
                    LatPoint = waterLeak.LatPoint,
                    LongPoint = waterLeak.LongPoint,
                    Status = waterLeak.Status,
                    Topic = waterLeak.Topic,
                    UserId = (waterLeak.User != null) ? waterLeak.User.Username : String.Empty,
                    Resource1 = resource1,
                    Resource2 = resource2,
                    Resource3 = resource3
                };
            }
            return result;
        }
    }
}
